package com.fantastic.web.config;

import com.fantastic.web.interceptors.TokenAuthInterceptor;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.multipart.commons.CommonsMultipartResolver;
import org.springframework.web.servlet.config.annotation.*;

/**
 * @author: rxx
 * @since: 2024/5/22 10:35
 * @description: springMVC核心配置类。
 */
//标明为配置类
@Configuration
//扫描控制器包和全局异常处理包
@ComponentScan({"com.fantastic.web.controller", "com.fantastic.web.ex"})
//开启默认servlet和拦截器和扫描springmvc的注解
@EnableWebMvc
public class SpringMvcConfig implements WebMvcConfigurer {

    /**
     * 配置文件解析器
     *
     * @return 返回文件解析器对象
     */
    @Bean
    public CommonsMultipartResolver multipartResolver() {
        //创建文件解析器对象
        CommonsMultipartResolver multipartResolver = new CommonsMultipartResolver();
        //文件编码方式
        multipartResolver.setDefaultEncoding("UTF-8");
        //最大文件上传大小  单位字节 5mb    5242880     10485760
        multipartResolver.setMaxUploadSize(5242880);
        return multipartResolver;
    }

    //开启DefaultServlet，可以处理静态资源了
    @Override
    public void configureDefaultServletHandling(DefaultServletHandlerConfigurer configurer) {
        //开启默认的servlet处理器
        configurer.enable();
    }

    /**
     * 添加拦截器
     * addPathPatterns：对/app/**下的所有请求进行拦截
     * excludePathPatterns：对指定路径进行放行
     *
     * @param registry
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
//        registry.addInterceptor(new TokenAuthInterceptor())
//                .addPathPatterns("/app/**")
//                .excludePathPatterns("/app/login", "/app/register","app/listIntegralVoApp");
    }

    /**
     * 跨域处理
     *
     * @param registry
     */
    @Override
    public void addCorsMappings(CorsRegistry registry) {
        registry.addMapping("/**")
                .allowedOrigins("*")
                .allowedMethods("*")
                .allowedHeaders("*");
    }

}
